package com.cuit.ocp.controller;

import com.alibaba.fastjson.JSONArray;
import com.cuit.ocp.bean.Trader;
import com.cuit.ocp.service.TraderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins ="*",maxAge = 3600)
@RestController
@RequestMapping("trader")
public class TraderController {

    @Autowired
    private TraderService traderService;

    @GetMapping("findAll")
    public Map<Object,Object> findAll(@RequestParam(value = "page_num",defaultValue = "1")int page_num){
        Map<Object,Object> result = new HashMap<>();
        result = traderService.findAll(page_num);
        return result;
    }

    @GetMapping("findById")
    public HashMap<Object,Object> findStaffById(int tid){
        HashMap<Object,Object> result = new HashMap<>();
        List<Trader> trader_info = traderService.findById(tid);
        if(trader_info != null){
            result.put("new_staff",trader_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @GetMapping("findByName")
    public HashMap<Object,Object> findByName( Trader trader){
        HashMap<Object,Object> result = new HashMap<>();
        List<Trader> trader_info = traderService.findByName(trader.getTradername());
        if(trader_info != null){
            result.put("new_staff",trader_info);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @PostMapping("addNewTrader")
    public HashMap<Object,Object> addNewStaff(@RequestBody String newTrader_info){
        HashMap<Object,Object> result = new HashMap<>();
        Trader trader = traderService.jsonToTrader(newTrader_info);
        if(traderService.addNewTrader(trader) == true){
            result.put("new_staff",trader);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }


    @PostMapping("update")
    public HashMap<Object,Object> update(@RequestBody String newTrader_info){
        HashMap<Object,Object> result = new HashMap<>();
        Trader trader = traderService.jsonToTrader(newTrader_info);
        if(traderService.update(trader) == true){
            result.put("new_staff",trader);
            result.put("message","success");
        }
        else{
            result.put("message","failure");
        }
        return result;
    }

    @GetMapping("delete")
    public HashMap<Object,Object> deleteById(int tid){
        HashMap<Object,Object> result = new HashMap<>();
        if(traderService.deleteById(tid) == true)
            result.put("message","success");
        else
            result.put("message","failure");
        return result;
    }
    @PostMapping("deletes")
    public HashMap<Object,Object> deleteByIds(@RequestBody String trader_ids){
        HashMap<Object,Object> result = new HashMap<>();
        JSONArray json_staffIDs= JSONArray.parseArray(trader_ids);
        List<Integer> ids = new ArrayList<>();
        for(int i=0;i<json_staffIDs.size();i++)
            ids.add((Integer) json_staffIDs.get(i));
        if(traderService.deleteByIds(ids) == true)
            result.put("message","success");
        else
            result.put("message","failure");
        return result;
    }
}
